package com.maomao.time;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by admin on 2018/5/11.
 */
public class SeasonTime {
    public static void main(String[] args) {
        Date a= strToDate("2013-04-16");
//        System.out.println(getThisSeasonTime(new Date()));
        System.out.println(nextSeasonEndDay(new Date()));

    }

    public static String nextSeasonEndDay(Date date) {
        String nextSeasonEnd ="";
        int month = getMonth(new Date());
        int year = getYear(new Date());
        if(month>=1&&month<=3){
            nextSeasonEnd = year +"-6-30";
        }
        if(month>=4&&month<=6){
            nextSeasonEnd = year +"-9-30";
        }
        if(month>=7&&month<=9){
            nextSeasonEnd = year +"-12-31";
        }
        if(month>=10&&month<=12){
            nextSeasonEnd = (year+1) +"-3-31";
        }
//        System.out.println(nextSeasonEnd);
        return nextSeasonEnd;
    }

    public static int getYear(Date date){
        Calendar Cal=Calendar.getInstance();
        Cal.setTime(date);
        return Cal.get(Calendar.YEAR);
    }
    /**
     * 获取某年某月的最后一天
     * @param year 年
     * @param month 月
     * @return 最后一天
     */
    private static int getLastDayOfMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
                || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            if (isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        }
        return 0;
    }

    /**
     * 是否闰年
     * @param year 年
     * @return
     */
    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    public static int getMonth(Date date){
        Calendar Cal=Calendar.getInstance();
        Cal.setTime(date);
        return Cal.get(Calendar.MONTH);
    }
    //获得本季度
    public static String getThisSeasonTime(Date date){
        int month= getMonth(date);
        int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
        int season = 1;
        if(month>=1&&month<=3){
            season = 1;
        }
        if(month>=4&&month<=6){
            season = 2;
        }
        if(month>=7&&month<=9){
            season = 3;
        }
        if(month>=10&&month<=12){
            season = 4;
        }
//        int start_month = array[season-1][0];
        int end_month = array[season-1][2];

//        Date date = new Date();
        //可以方便地修改日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        String  years  = dateFormat.format(date);
        int years_value = Integer.parseInt(years);

//        int start_days =1;
        int end_days = getLastDayOfMonth(years_value,end_month);
        String seasonDate =years_value+"-"+end_month+"-"+end_days;
        return seasonDate;

    }

    /**
     * 字符串转时间
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
}
